import { Request, Response } from 'express';

// 模拟权限数据
const permissions: any[] = [
  { id: 1, name: '查看', description: '查看权限' },
  { id: 2, name: '编辑', description: '编辑权限' },
  { id: 3, name: '删除', description: '删除权限' },
];

// 模拟分页数据
const getPageData = (params: any) => {
  const { current = 1, pageSize = 10 } = params;
  const start = (current - 1) * pageSize;
  const end = start + Number(pageSize);
  return {
    content: permissions.slice(start, end),
      totalElements: permissions.length,
    current: Number(current),
    pageSize: Number(pageSize),
  };
};

export default {
  // 模拟获取权限列表接口
  'GET /api/system/permission': (req: Request, res: Response) => {
    const pageData = getPageData(req.query);
    res.status(200).json({
      code: 200,
      message: '获取权限列表成功',
      data: pageData,
    });
  },

  // 模拟添加权限接口
  'POST /api/system/permission': (req: Request, res: Response) => {
    const newPermission = req.body;
    newPermission.id = permissions.length + 1;
    permissions.push(newPermission);
    res.status(200).json({
      code: 200,
      message: '添加权限成功',
      data: newPermission,
    });
  },

  // 模拟更新权限接口
  'PUT /api/system/permission': (req: Request, res: Response) => {
    const updatedPermission = req.body;
    const index = permissions.findIndex((permission) => permission.id === updatedPermission.id);
    if (index !== -1) {
      permissions[index] = updatedPermission;
      res.status(200).json({
        code: 200,
        message: '更新权限成功',
        data: updatedPermission,
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '权限不存在',
      });
    }
  },

  // 模拟删除权限接口
  'DELETE /api/system/permission/:id': (req: Request, res: Response) => {
    const id = parseInt(req.params.id, 10);
    const index = permissions.findIndex((permission) => permission.id === id);
    if (index !== -1) {
      permissions.splice(index, 1);
      res.status(200).json({
        code: 200,
        message: '删除权限成功',
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '权限不存在',
      });
    }
  },

  // 模拟根据权限 ID 获取单个权限详细信息接口
  'GET /api/system/permission/:id': (req: Request, res: Response) => {
    const id = parseInt(req.params.id, 10);
    const permission = permissions.find((permission) => permission.id === id);
    if (permission) {
      res.status(200).json({
        code: 200,
        message: '获取权限详情成功',
        data: permission,
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '权限不存在',
      });
    }
  },

  // 模拟获取权限树形结构接口
  'GET /api/system/permission/tree': (req: Request, res: Response) => {
    // 这里简单返回全部权限作为树形结构，实际可按需调整
    res.status(200).json({
      code: 200,
      message: '获取权限树形结构成功',
      data: permissions,
    });
  },
  // 模拟获取权限选项接口
  'GET /api/system/permission/options': (req: Request, res: Response) => {
    const options = permissions.map((permission) => ({ id: permission.id, name: permission.name }));
    res.status(200).json({
      code: 200,
      message: '获取权限选项成功',
      data: options,
    });
  },
    // 模拟获取权限选项接口
    'GET /api/system/menu': (req: Request, res: Response) => {
        const options = permissions.map((permission) => ({ id: permission.id, name: permission.name }));
        res.status(200).json({
            code: 200,
            message: '获取权限选项成功',
            data: options,
        });
    },

};
